package lyo.levelup.thread;
public class CipherVigenere {
    private final int size = 26;
        private char [][] alphaBet = new char [size][size];
        private char [][] alphaBetVizhiner = new char [size][size];
        private String res = "";
        private int key = 0;
        private int num = 0;
        private char temp;
        private String str;
        private String keyStr;
        public CipherVigenere(String str, String secString)
        {
            this.str = str;
            this.keyStr = secString;
        }
        private String enCrString()
    {        
        while(keyStr.length() < str.length())
        {
            keyStr+=keyStr.charAt(num);
            num++;
        }        
        for(int i = 0; i < alphaBet.length; i++)
        {
            char c = 'A';
            for(int j = 0; j < alphaBet.length; j++)
            {
                alphaBet[i][j] = c;
                c++;
            }
        }      
        for(int i = 0; i < alphaBetVizhiner.length; i++)
        {         
         for(int j = 0; j < alphaBetVizhiner.length; j++)
            {
                if(j < key)
                {
                    alphaBetVizhiner[i][alphaBetVizhiner.length-1-j] = alphaBet [i][j];
                }
                else
                {
                    System.arraycopy(alphaBet[i], j, alphaBetVizhiner[i], j-key, 1);
                }
            }
         key++;
        }
        key=0;
        for(int i = 0; i < alphaBetVizhiner.length; i++)
        {            
         for(int j = alphaBetVizhiner.length-key; j < alphaBetVizhiner.length; j++)
            {
               for(int k = j+1; k < alphaBetVizhiner.length; k++) 
               {
                   temp=alphaBetVizhiner[i][j];                  
                    alphaBetVizhiner[i][j]=alphaBetVizhiner[i][k];
                    alphaBetVizhiner[i][k]=temp;
               }
            }
         key++;
        }  
        for(int i = 0; i < str.length(); i++)
        {
            for(int j = 0; j < size; j++)
            {
                if(str.charAt(i) == alphaBetVizhiner[0][j])
                {
                   for(int k = 0; k < size; k++)
                   {
                       if(keyStr.charAt(i) == alphaBetVizhiner[k][0])
                       {
                          res+=alphaBetVizhiner[k][j]; 
                       }
                   }
                }
            }
        }
        return res;
    }
     public void output()
    {
        enCrString();
        System.out.println("You encrypt word is "+this.enCrString());       
    }
}